package com.rd.student.jike.class2.lihongming.lock718;

/**
 * @Auther:Heinrich
 * @Date:2023/7/18-07-18-21:56
 * @Description:com.rd.student.jike.class2.lihongming.lock718
 * @version:1.0
 */
public class TestBank2 extends Thread{


    //账户来取钱，构成类的组合关系


    Account2 account;
    double money;
    public TestBank2(String name, Account2 account, double money) {
        super(name);
        this.account = account;
        //System.out.println(account.getBalance()+" "+getName());
        this.money = money;
    }
    //取钱
    @Override
    public void run() {
        account.drawMoney(account,money);
    }

    public static void main(String[] args) throws InterruptedException {
        Account2 account2 = new Account2("123456",888);
        new TestBank2("张三",account2,666).start();
        new TestBank2("李四",account2,666).start();
        new TestBank2("王五",account2,666).start();
        Thread.sleep(2000);
        System.out.println(account2.getBalance());
    }
}
class Account2{
    private String account0ID;
    private double balance;

    public Account2() {
    }

    public Account2(String account0ID, double balance) {
        this.account0ID = account0ID;
        this.balance = balance;
    }
    public void drawMoney(Account2 account,double money){
        synchronized (this){
            if(account.getBalance()-money<0){
                System.out.println(Thread.currentThread().getName()+" 不能取钱，余额不足");
            }else{
                System.out.println(Thread.currentThread().getName()+" 可以取钱");
                account.setBalance(account.getBalance()-money);
                System.out.println("取钱后的余额为："+account.getBalance());
            }
        }
    }
    public String getAccount0ID() {
        return account0ID;
    }

    public void setAccount0ID(String account0ID) {
        this.account0ID = account0ID;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
